﻿// /*
//  * @Author: Dreamy Icecream 
//  * @Date: 2020-12-31 11:23:55 
//  * @Description: 边缘检测
//  * @Last Modified by: Dreamy Icecream 
//  * @Last Modified time: 2020-12-31 11:23:55 
//  */

Shader "Custom/EdgeDetection" {

    Properties {
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _EdgeOnly ("边缘显示的比重", Float) = 1.0
        _EdgeColor ("边缘颜色", Color) = (0, 0, 0, 1)
        _BackgroundColor ("背景颜色", Color) = (1, 1, 1, 1)
    }

    SubShader {

        Pass{
            ZTest Always Cull Off ZWrite Off

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment fragSobel

            #include "UnityCG.cginc"

            sampler2D _MainTex;
            half4 _MainTex_TexelSize;
            fixed _EdgeOnly;
            fixed4 _EdgeColor;
            fixed4 _BackgroundColor;

            struct v2f{
                float4 pos : SV_POSITION;
                half2 uv[9] : TEXCOORD0;
            };

            v2f vert(appdata_img v) {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);

                half2 uv = v.texcoord;

                o.uv[0] = uv + _MainTex_TexelSize.xy * half2(-1, -1);
                o.uv[1] = uv + _MainTex_TexelSize.xy * half2(0, -1);
                o.uv[2] = uv + _MainTex_TexelSize.xy * half2(1, -1);
                o.uv[3] = uv + _MainTex_TexelSize.xy * half2(-1, 0);
                o.uv[4] = uv + _MainTex_TexelSize.xy * half2(0, 0);
                o.uv[5] = uv + _MainTex_TexelSize.xy * half2(1, 0);
                o.uv[6] = uv + _MainTex_TexelSize.xy * half2(-1, 1);
                o.uv[7] = uv + _MainTex_TexelSize.xy * half2(0, 1);
                o.uv[8] = uv + _MainTex_TexelSize.xy * half2(1, 1);

                return o;
            }

            /** 饱和度数据 */
            fixed luminance(fixed4 color) {
                return 0.2125 * color.r + 0.7154 * color.g + 0.0721 * color.b;
            }

            /** 像素梯度 */
            half Sobel(v2f i){
                const half Gx[9] = {
                    -1, -2, -1,
                    0, 0, 0,
                    1, 2, 2
                };
                const half Gy[9] = {
                    -1, 0, 1,
                    -2, 0, 2,
                    -1, 0, 1
                };

                half texColor;
                half edgeX = 0;
                half edgeY = 0;
                
                for(int index = 0; index < 9; index++) {
                    texColor = luminance(tex2D(_MainTex, i.uv[index]));
                    edgeX += texColor * Gx[index];
                    edgeY += texColor * Gy[index];
                }

                half edge = 1 - abs(edgeX) / 18 - abs(edgeY) / 18;

                return edge;
            }
            
            fixed4 fragSobel(v2f i) : SV_TARGET {
                half edge = Sobel(i);

                fixed4 withEdgeColor = lerp(_EdgeColor, tex2D(_MainTex, i.uv[4]), edge);
                fixed4 onlyEdgeColor = lerp(_EdgeColor, _BackgroundColor, edge);
                return lerp(withEdgeColor, onlyEdgeColor, _EdgeOnly);
            }

            ENDCG
        }
    }

    FallBack Off    
}
